<template>
  <view class="order-list-page">
    <!-- 顶部状态筛选 -->
    <scroll-view class="tabs" scroll-x>
      <view
        v-for="tab in tabs"
        :key="tab.value"
        class="tab-item"
        :class="{ active: currentStatus === tab.value }"
        @click="switchStatus(tab.value)"
      >
        {{ tab.label }}
      </view>
    </scroll-view>

    <!-- 列表区域 -->
    <scroll-view class="list" scroll-y @scrolltolower="loadMore">
      <view class="card" v-for="item in list" :key="item.id" @click="goDetail(item)">
        <view class="card-hd">
          <text class="order-no">订单号：{{ item.order_no }}</text>
          <text class="status">{{ item.status_text }}</text>
        </view>
        <view class="goods" v-if="item.goods && item.goods.length">
          <view class="good" v-for="g in item.goods" :key="g.id">
            <image class="thumb" :src="g.image" mode="aspectFill" />
            <view class="gi">
              <text class="name">{{ g.name }}</text>
              <text class="meta">¥{{ g.price }} x {{ g.quantity }}</text>
            </view>
          </view>
        </view>
        <view class="card-ft" @click.stop>
          <text class="amount">合计：¥{{ item.total_amount }}</text>
          <view class="actions">
            <button v-if="item.status === 'pending_pay'" class="btn pay" @tap="pay(item)">去支付</button>
            <button v-if="item.status === 'pending_pay'" class="btn cancel" @tap="cancel(item)">取消订单</button>
            <button class="btn detail" @tap="goDetail(item)">查看详情</button>
          </view>
        </view>
      </view>

      <view class="loadmore" v-if="hasMore">
        <text>{{ loading ? '加载中…' : '上拉加载更多' }}</text>
      </view>
      <view class="empty" v-if="!loading && list.length === 0">
        <image class="empty-img" src="https://placehold.co/200x200/png?text=%E6%97%A0%E8%AE%A2%E5%8D%95" mode="aspectFit" />
        <text class="empty-txt">暂无订单</text>
      </view>
    </scroll-view>
  </view>
  
</template>

<script setup>
// 中文注释：订单列表页
import { ref, getCurrentInstance } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { orderApi } from '@/api/index.js'

const { proxy } = getCurrentInstance()

const tabs = [
  { label: '全部', value: '' },
  { label: '待支付', value: 'pending_pay' },
  { label: '待接单', value: 'pending_accept' },
  { label: '配送中', value: 'delivering' },
  { label: '已完成', value: 'completed' }
]

const currentStatus = ref('')
const list = ref([])
const page = ref(1)
const pageSize = ref(10)
const hasMore = ref(true)
const loading = ref(false)

const fetchList = async () => {
  if (loading.value || !hasMore.value) return
  loading.value = true
  try {
    const data = await orderApi.getOrderList({ status: currentStatus.value, page: page.value, page_size: pageSize.value })
    const items = data.list || []
    list.value = page.value === 1 ? items : [...list.value, ...items]
    hasMore.value = items.length === pageSize.value
    page.value++
  } catch (e) {
    proxy?.$showToast?.('加载失败')
  } finally {
    loading.value = false
  }
}

const switchStatus = (status) => {
  if (currentStatus.value === status) return
  currentStatus.value = status
  page.value = 1
  hasMore.value = true
  list.value = []
  fetchList()
}

const loadMore = () => fetchList()

const goDetail = (item) => {
  proxy?.$navigateTo?.(`/pages/order/detail?id=${item.id}`)
}

const pay = async (item) => {
  try {
    await orderApi.payOrder({ order_id: item.id })
    uni.showToast({ title: '支付成功', icon: 'success' })
    setTimeout(() => goDetail(item), 800)
  } catch (e) {}
}

const cancel = (item) => {
  uni.showModal({
    title: '提示',
    content: '确定取消该订单吗？',
    success: async (res) => {
      if (res.confirm) {
        try {
          await orderApi.cancelOrder(item.id, '用户取消')
          uni.showToast({ title: '已取消', icon: 'none' })
          // 刷新列表
          page.value = 1
          hasMore.value = true
          list.value = []
          fetchList()
        } catch (e) {}
      }
    }
  })
}

onLoad(() => {
  fetchList()
})
</script>

<style scoped lang="scss">
.order-list-page { min-height: 100vh; background: $uni-bg-color-grey; }
.tabs { white-space: nowrap; background: #fff; padding: 16rpx 20rpx; border-bottom: 1px solid $uni-border-color; }
.tab-item { display: inline-block; padding: 12rpx 24rpx; margin-right: 16rpx; font-size: 26rpx; color: #666; background: #f5f5f5; border-radius: 999rpx; border: 1rpx solid $uni-border-color; }
.tab-item.active { color: #fff; background: $uni-color-primary; border-color: $uni-color-primary; }
.list { height: calc(100vh - 100rpx); padding: 20rpx; box-sizing: border-box; }
.card { background: #fff; border-radius: 16rpx; margin-bottom: 20rpx; overflow: hidden; box-shadow: 0 12rpx 32rpx rgba(0,0,0,0.06); }
.card-hd { padding: 20rpx; display: flex; justify-content: space-between; color: #333; border-bottom: 1px solid $uni-border-color; }
.order-no { font-size: 26rpx; color: #666; }
.status { font-size: 26rpx; color: $uni-color-primary; }
.goods { padding: 10rpx 20rpx; }
.good { display: flex; align-items: center; padding: 12rpx 0; }
.thumb { width: 120rpx; height: 120rpx; border-radius: 12rpx; background: #f5f5f5; }
.gi { flex: 1; margin-left: 16rpx; }
.name { font-size: 28rpx; color: #333; }
.meta { font-size: 24rpx; color: #999; margin-top: 6rpx; }
.card-ft { padding: 20rpx; display: flex; justify-content: space-between; align-items: center; border-top: 1px solid $uni-border-color; }
.amount { color: #333; font-size: 28rpx; }
.actions { display: flex; gap: 12rpx; }
.btn { height: 64rpx; line-height: 64rpx; padding: 0 24rpx; border-radius: 64rpx; border: none; font-size: 26rpx; }
.btn.pay { background: $uni-color-primary; color: #fff; }
.btn.cancel { background: #f5f5f5; color: #333; }
.btn.detail { background: #fff5e6; color: $uni-color-primary; }
.loadmore { text-align: center; color: #999; padding: 20rpx 0; }
.empty { text-align: center; color: #999; padding: 60rpx 0; }
.empty-img { width: 200rpx; height: 200rpx; margin-bottom: 16rpx; }
.empty-txt { font-size: 26rpx; }
</style>
